<template>
  <search-wrap
    :schema="searchSchema"
    @reset="setSearchParams"
    @search="setSearchParams"
  />
  <table-wrap
    :pagination="pagination"
    :columns="tableColumns"
    :data="dataList"
    :loading="loading"
    @register="tableRegister"
  />
</template>
<script setup lang="tsx">
  import { Link } from '@arco-design/web-vue';

  import { listApi, removeApi } from '@/api/system/excel-import';
  import type { ExcelImportVO } from '@/api/system/excel-import/types';
  import { useTable } from '@/hooks/table';

  const { tableRegister, tableState, tableMethods } = useTable({
    fetchDataApi: async () => {
      const { pageParams } = tableState;
      return await listApi({
        ...pageParams.value,
        ...unref(searchParams),
      });
    },
    fetchDelApi: async () => {
      const res = await removeApi(unref(currentId));
      return !!res;
    },
  });

  const { dataList, loading, pagination } = tableState;
  const { getList, delList } = tableMethods;

  const tableColumns = reactive<TableColumn[]>([
    {
      field: 'index',
      title: '序号',
      type: 'index',
    },
    {
      field: 'type',
      title: '类型',
      type: 'enumTag',
      enumName: 'ExcelImportType',
    },
    {
      field: 'title',
      title: '标题',
    },
    {
      field: 'status',
      title: '状态',
      type: 'enumTag',
      enumName: 'ExcelImportStatus',
    },
    {
      field: 'createTime',
      title: '创建时间',
    },
    {
      field: 'successCount',
      title: '成功条数',
    },
    {
      field: 'failCount',
      title: '失败条数',
    },
    {
      field: 'action',
      title: '操作',
      width: 200,
      render: (data: any) => {
        const row = data.record as ExcelImportVO;
        return (
          <>
            <Link disabled={row.status === 3} onClick={() => delAction(row.id)}>
              删除
            </Link>
          </>
        );
      },
    },
  ]);

  const searchSchema = reactive<FormSchema[]>([
    {
      field: 'status',
      label: '状态',
      type: 'Select',
      enumName: 'ExcelImportStatus',
    },
  ]);

  const searchParams = ref({});
  const setSearchParams = (data: any) => {
    searchParams.value = data;
    getList(false);
  };

  const currentId = ref<string>();

  const delAction = async (id: string) => {
    currentId.value = id;
    await delList(1).finally(() => {});
  };
</script>
